-
Notifications
You must be signed in to change notification settings - Fork 103
[sql-33] session: add migration code from kvdb to SQL #1051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[sql-33] session: add migration code from kvdb to SQL #1051
Conversation
449a2cc
to
1828ec4
Compare
In preparation for upcoming migration tests from a kvdb to an SQL store, this commit updates the NewTestDB function to return the Store interface rather than a concrete store implementation. This change ensures that migration tests can call NewTestDB under any build tag while receiving a consistent return type.
When a session has a MacaroonRecipe set, which has a nil value set for either the `Permissions` or the `Caveats` field, the kvdb session store would return that field as nil, while the sql prior to this commit would return an empty array. When the we migrate the session store from kvdb to sql, that will cause the kvdb and the migrate session store to differ when comparing them, which will cause the migration to fail. This commit fixes that by ensuring that the sql session store returns a nil value for those fields in that scenario, rather than an empty array.
This commit introduces the migration logic for transitioning the sessions store from kvdb to SQL. Note that as of this commit, the migration is not yet triggered by any production code, i.e. only tests execute the migration logic.
1828ec4
to
a868a93
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding some review comments where input would be extra helpful :)!
macRecipe = &MacaroonRecipe{} | ||
if perms != nil { | ||
macRecipe.Permissions = unmarshalMacPerms(perms) | ||
} | ||
if caveats != nil { | ||
macRecipe.Caveats = unmarshalMacCaveats(caveats) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you think we should rather fix the kvdb store implementation rather than the sql one. But as we've previously opted for not updating the store behaviour, and make the sql one mimic the behaviour until we've completed the migration, I opted for this version here.
if !session.RevokedAt.IsZero() { | ||
err = tx.SetSessionRevokedAt( | ||
ctx, | ||
makeSetRevokedAtParams(sqlId, session.RevokedAt), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could potentially update the insert session sql query to also take an optional RevokedAt sql.NullTime
param, to not have to do this as a separate step. I opted for this version to keep things as is as much as possible, but if you think that'd be better, I'll update to do that instead :)
{ | ||
"randomized sessions", | ||
randomizedSessions, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted for a test that uses the "standard" rand
lib here, as @bitromortac had a preference for that in #1047 (comment). But can also look into adding a test that uses rapid
if you'd prefer.
// We always have a caveat.Id, but the rest are | ||
// randomized if they exist or not. | ||
macCaveat.Id = randomBytes(rand.Intn(10) + 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if this is not true in for real world data, as migrations WILL fail if we don't set a caveat.Id
when the caveats
param of WithMacaroonRecipe
is not nil
.
// "dest" state, without checking if the state transition is legal. | ||
// | ||
// NOTE: this function should only be used for testing purposes. | ||
func (db *BoltStore) shiftStateUnsafe(_ context.Context, id ID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not fully sure if we add test functions like this one that's a struct
member to the _test
file, or if we add it in the actual struct file?
This PR introduces the migration logic for transitioning the sessions store from kvdb to SQL.
Note that as of this PR, the migration is not yet triggered by any production code, i.e. only tests execute the migration logic.
Part of #917